#include "widget.h"
#include "QtTool/qttoolwidget.h"
#include "RetractableSidebarWindow/sidebaroptionsbutton.h"
#include "Set/setwidget.h"
#include "Prompter/prompterwidget.h"

Widget::Widget(QWidget *parent)
    : SidebarWindow(parent)
{
    setupSidebar(sidebar());
    setupStackedWidget(stackedWidget());
    connectSignals();
    setMinimumHeight(sidebar()->childrenCumulativeHeight());// 设置窗口最小高度为侧边栏所有控件的累计高度,确保窗口缩放时侧边栏中的控件不会重叠
    resize(680,360);
}

Widget::~Widget() {}

void Widget::setupSidebar(Sidebar *parent)
{
    btn_qt = new SidebarOptionsButton(parent);
    btn_prompter = new SidebarOptionsButton(parent);
    verticalSpacer = new QSpacerItem(0, 0, QSizePolicy::Policy::Minimum, QSizePolicy::Policy::Expanding);
    btn_set = new SidebarOptionsButton(parent);
    btn_expand = new SidebarOptionsButton(parent);

    //添加到侧边栏（注意顺序，从上到下）
    parent->addWidget(btn_qt);
    parent->addWidget(btn_prompter);
    parent->addItem(verticalSpacer);
    parent->addWidget(btn_set);
    parent->addWidget(btn_expand);

    //设置图标
    btn_qt->setIcon(QPixmap(":/button_icon/qt-white.svg"));
    btn_prompter->setIcon(QPixmap(":/button_icon/head-lightbulb.svg"));
    btn_set->setIcon(QPixmap(":/button_icon/gear.svg"));
    btn_expand->setIcon(QPixmap(":/button_icon/three_lines.svg"));

    // 设置右侧文本
    btn_qt->setText("Qt-uic、qrc转换工具");
    btn_prompter->setText("提词工具");
    btn_set->setText("设置");

    // 设置选中提示线颜色
    btn_qt->setPromptLineColor(QColor(65,205,82));
    btn_prompter->setPromptLineColor(QColor(255,214,0));
    btn_set->setPromptLineColor(QColor(113, 124, 134));

    //其他设置
    btn_qt->setChecked(true);                   // 默认第一个按钮被选中
    btn_expand->setFixedSize(40, 36);           // 展开按钮固定大小
    btn_expand->setDrawPromptLineEnable(false); // 展开按钮不绘制选中提示线条
    btn_expand->setCheckable(false);            // 展开按钮不可选中(可点击但不可选中)
}

void Widget::setupStackedWidget(QStackedWidget *parent)
{
    //初始化子页面
    qt_tool_widget = new QtToolWidget(parent);
    set_widget = new SetWidget(parent);
    prompter_widget = new PrompterWidget(parent);


    //添加子页面（注意添加顺序和按钮顺序一致）
    parent->addWidget(qt_tool_widget);
    parent->addWidget(prompter_widget);
    parent->addWidget(set_widget);
}

void Widget::connectSignals()
{
     //批量设置索引并连接页面切换信号
    int index = 0;
    for(auto& btn : sidebar()->findChildren<SidebarOptionsButton*>())
    {
        btn->setIndex(index++);
        connect(btn,&SidebarOptionsButton::selectedIndex,stackedWidget(),&QStackedWidget::setCurrentIndex);
    }

    connect(this->btn_expand, &SidebarOptionsButton::clicked, sidebar(), &Sidebar::autoExpand);//展开和收缩
    connect(this, &SidebarWindow::clicked, sidebar(), &Sidebar::shrink);//点击页面侧边栏收缩
}
